package com.hospital.user.cost.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hospital.user.cost.entity.UserCost;
import com.hospital.user.cost.service.UserCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cost")
public class UserCostController {

    @Autowired
    private UserCostService userCostService;

    @RequestMapping("/index")
    String index(Model model, String name, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        List<UserCost> userCosts = userCostService.UserSelect(name, pageNum, pageSize);
        PageInfo<UserCost> page = new PageInfo<>(userCosts);
        model.addAttribute("page", page);
        model.addAttribute("name", name);
        return "Userr/UserPaymentSelect";
    }

    @RequestMapping("/bill")
    @ResponseBody
    String bill(Model model, Integer paymentId) {
        //查询余额
        Double money = userCostService.UserMoneySelect(paymentId);
        if (money >= 300) {
            boolean flag = true;
            //根据用户id获取我的用户信息
            List<UserCost> userCosts = userCostService.userPayment(paymentId);

            Double aDouble1 = userCostService.ctPrice(paymentId);  //查询缴费类型的价格

            int i = userCostService.userPaymentNbm(paymentId);  //查询同一类型 （有问题）

            int countInsert = 0;

            if (money > aDouble1) {
                countInsert = userCostService.userPaymentInsert(paymentId, "前台缴费", aDouble1 * i);
                int i1 = userCostService.userStateUpdate(paymentId);
            }
            if (countInsert < 0) {
                flag = false;
            }
            return JSON.toJSONString(flag);
        }
        return JSON.toJSONString(false);
    }

    @RequestMapping("/pay")
    @ResponseBody
    String pay(Integer id, Integer userId) {
        boolean pay = userCostService.pay(id, userId);
        return JSON.toJSONString(pay);
    }

}
